﻿package com.gengine.resource.core
{
    import com.gengine.debug.*;
    import com.gengine.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;

    public class ClientLoader extends Loader
    {
        protected var _isProgressCheckReload:Boolean = false;
        protected var _request:URLRequest;
        protected var _t:int;
        protected var _isCompl:Boolean = false;
        protected var _context:LoaderContext;

        public function ClientLoader(param1:Boolean = false)
        {
            this._isProgressCheckReload = param1;
            return;
        }// end function

        protected function onLoaderProgress(event:ProgressEvent) : void
        {
            if (this._isProgressCheckReload && event.bytesLoaded != event.bytesTotal)
            {
                this.checkReload(8000);
            }
            else if (!this._isCompl && event.bytesLoaded == event.bytesTotal)
            {
                this.checkReload(2000);
            }
            return;
        }// end function

        protected function checkReload(param1:int) : void
        {
            this.clearReload();
            this._t = setTimeout(this.reload, param1);
            return;
        }// end function

        protected function clearReload() : void
        {
            if (this._t)
            {
                clearTimeout(this._t);
                this._t = 0;
            }
            return;
        }// end function

        protected function reload() : void
        {
            Log.debugMsg(LogType.other, "ClientLoader", this.getUrl(), "重新加载", getTimer());
            var _loc_1:* = this.getUrl();
            this.removeEvent();
            this.unload();
            var _loc_2:* = _loc_1.indexOf("?");
            if (_loc_2 >= 0)
            {
                _loc_1 = _loc_1 + MathUitl.random(0, 9);
            }
            else
            {
                _loc_1 = _loc_1 + ("?v=" + MathUitl.random(0, 9));
            }
            this.load(new URLRequest(_loc_1), this._context);
            return;
        }// end function

        protected function onLoaderComplete(event:Event) : void
        {
            this.clearReload();
            this.removeEvent();
            this._isCompl = true;
            return;
        }// end function

        override public function load(param1:URLRequest, param2:LoaderContext = null) : void
        {
            this._request = param1;
            this._context = param2;
            this.clearReload();
            this._isCompl = false;
            this.addEvent();
            super.load(param1, param2);
            return;
        }// end function

        public function getUrl() : String
        {
            if (this._request)
            {
                return this._request.url;
            }
            return "";
        }// end function

        protected function addEvent() : void
        {
            this.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onLoaderProgress, false, 0, true);
            this.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoaderComplete, false, 0, true);
            return;
        }// end function

        protected function removeEvent() : void
        {
            this.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.onLoaderProgress, false);
            this.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onLoaderComplete, false);
            return;
        }// end function

    }
}
